package com.hoyo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hoyo.common.PageInfo;
import com.hoyo.domain.Channel;
import com.hoyo.mapper.ChannelMapper;

@Service
public class ChannelServiceImpl implements ChannelService{
	
	@Autowired
	private ChannelMapper mapper;

	@Override
	public List<Channel> findAllChannels() throws Exception{
		return mapper.findAll();
	}

	@Override
	public List<Channel> findAllChannelsAndCountMovieInEveryChannel() throws Exception {
		// TODO Auto-generated method stub
		return mapper.findAllChannelsAndCountMovieInEveryChannel();
	}

	@Override
	public PageInfo<Channel> pageFind(int pageNum, int pageSize) throws Exception {
		List<Channel> channels = mapper.findAllChannelsAndCountMovieInEveryChannelLimitBy((pageNum-1)*pageSize, pageSize);
		int total = channels.size()>0?channels.get(0).getTotal():0;
		return new PageInfo<>(pageNum, pageSize, total, channels);
	}

	@Override
	public PageInfo<Channel> save(int id, String channelName, String description) throws Exception {
		if(id<0){
			Channel channel = new Channel(channelName, description);
			mapper.save(channel);
		}else{
			mapper.update(id, channelName, description);
		}
		return this.pageFind(1, 10);
	}
}
